/*
Java board game library.
Copyright (C) 2011  Deepesh Garg

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package d.g.boardgames.chess;

import d.g.boardgames.Cell;
import d.g.boardgames.chess.ChessCellState.PieceType;

public class AlgebricNotationHelper {
	public static String getCellString (int row, int col) {
		String cellString = "";
		cellString += getFileString(col);
		cellString += getRankString(row);
		return cellString;
	}
	
	public static String getCellString (Cell cell) {
		return getCellString(cell.getRow(), cell.getCol());
	}
	
	public static Cell getCell (String cellString) {
		if (cellString.length() < 2) {
			return null;
		}
		char colChar = cellString.charAt(0);
		String rowStr = cellString.substring(1,2);
		int row = 0; int col = 0;
		switch (colChar) {
			case 'a': case 'A': col = 0; break;
			case 'b': case 'B': col = 1; break;
			case 'c': case 'C': col = 2; break;
			case 'd': case 'D': col = 3; break;
			case 'e': case 'E': col = 4; break;
			case 'f': case 'F': col = 5; break;
			case 'g': case 'G': col = 6; break;
			case 'h': case 'H': col = 7;
		}
		row = 8 - Integer.parseInt(rowStr);
		return new Cell(row, col);
	}

	public static String getFileString (int col) {
		String fileString = "";
		switch (col) {
			case 0: fileString += "a"; break;
			case 1: fileString += "b"; break;
			case 2: fileString += "c"; break;
			case 3: fileString += "d"; break;
			case 4: fileString += "e"; break;
			case 5: fileString += "f"; break;
			case 6: fileString += "g"; break;
			case 7: fileString += "h"; break;
		}
		return fileString;
	}
	
	public static int getCol (char file) {
		switch (file) {
			case 'a': return 0;
			case 'b': return 1;
			case 'c': return 2;
			case 'd': return 3;
			case 'e': return 4;
			case 'f': return 5;
			case 'g': return 6;
			case 'h': return 7;
		}
		return 0;
	}

	public static int getRow (char rank) {
		return 8 - Integer.parseInt("" + rank);
	}
	
	public static String getFileString (Cell cell) {
		return getFileString(cell.getCol());
	}
	
	public static String getRankString (int row) {
		return "" + (8 - row);
	}
	
	public static String getRankString (Cell cell) {
		return getRankString(cell.getRow());
	}
	
	public static String getPieceString (PieceType piece) {
		switch (piece) {
			case PAWN: return "";
			case KNIGHT: return "N";
			case BISHOP: return "B";
			case ROOK: return "R";
			case QUEEN: return "Q";
			case KING: return "K";
			default : return "";
		}
	}
	
	public static PieceType getPieceType (String pieceString) {
		if (pieceString.equalsIgnoreCase("n")) {
			return PieceType.KNIGHT;
		}
		if (pieceString.equalsIgnoreCase("b")) {
			return PieceType.BISHOP;
		}
		if (pieceString.equalsIgnoreCase("r")) {
			return PieceType.ROOK;
		}
		if (pieceString.equalsIgnoreCase("q")) {
			return PieceType.QUEEN;
		}
		if (pieceString.equalsIgnoreCase("k")) {
			return PieceType.KING;
		}
		return PieceType.PAWN;
	}
}
